<?php
/**
 * Events View
 * @author 		TAMDV
 * @date		2008 07 28 (yyyy MM dd)
 * @package    	Joomus.JBS
 * @subpackage 	Components
 * @link 		http://localhost/jbschedule/administrator/index.php?option=com_jbschedule
 * @license		GNU/GPL
 */

// no direct access
//defined('_JEXEC') or die('Restricted access');


jimport ( 'joomla.application.component.view' );

/**
 * Events View
 *
 * @package    Joomus.JBS
 * @subpackage Components
 */
class JBSViewEvents extends JView {

	function display($tpl = null) {
		$task = JRequest::getVar ( 'task' );

		switch ($task) {
			case 'edit':
			case 'add' :
				//called model events


				//get the events
				$events = & $this->get ('DataOne');
				$isNew = ($events->id < 1);

				$text = $isNew ? JText::_ ( 'NEW' ) : JText::_ ( 'EDIT' );
				if ($isNew){
					$events->recur_type=1;
				}
				JToolBarHelper::title ( JText::_('PROJECT NAME').' - '.JText::_ ( 'EVENTS' ) . ': <small><small>[ ' . $text . ' ]</small></small>', 'jbschedule' );
				JToolBarHelper::save ();
				if ($isNew) {
					JToolBarHelper::cancel ();
				} else {
					// for existing items the button is renamed `close`
					JToolBarHelper::cancel ( 'cancel', 'Close' );
				}

				$modelEvent = $this->getModel('events');

				//fill data into combox eventtpe;
				$fillDataEventtype = '';
				$dataEventTypes = $modelEvent->getAllEventTypes();
				if (count($dataEventTypes) > 0){
					$fillDataEventtype.= "<select name='eventtype_id' style='width:150px'>";

					for ($i=0;$i<count($dataEventTypes);$i++){
						$checkedEventType = ($dataEventTypes[$i]->id == $events->eventtype_id) ? "selected='selected'" :"";
						$fillDataEventtype.="<option ".$checkedEventType." value=".$dataEventTypes[$i]->id.">".$dataEventTypes[$i]->title."</option>";
					}
					$fillDataEventtype.= "</select>";
				}

				//fill asset group
				$fillAsset 			= "";
				$dataAssetGroup 	= $modelEvent->getAllAssetgroups();
				$dataEventsAssets 	= $modelEvent->getAllEventsAssetsBy($events->id);
				if (count($dataAssetGroup) > 0){
					$fillAsset .= "<select size='9' name='assetId[]' multiple style='width: 150px;'>";
					foreach ($dataAssetGroup as $ag){
						$fillAsset .="<optgroup label=".$ag->name.">";
						$dataAsset = $modelEvent->getAllAssetByIdGroup($ag->id);
						foreach ($dataAsset as $a){
							$checkedAsset = $this->checkIdAssetInArray($a->id,$dataEventsAssets) ? "selected='selected'" :"";
							$fillAsset .="<option ".$checkedAsset." value=".$a->id.">".$a->name."</option>";
						}
						$fillAsset .="</optgroup>";
					}
					$fillAsset .="</select>";
				}
				
				//In Role
				$htmlRole = "<table border=\"0\"> \n";
				$htmlRole .= "<tr> \n";
				$htmlRole .= "<td> \n";
				$htmlRole .= "<select size=\"6\" id=\"tableOne\" name=\"tableOne[]\" multiple style=\"width: 200px\">";				
				$arrUser   = $modelEvent->getAllUserIsEventUser($events->id);
				if (count($arrUser) > 0) {
					foreach ($arrUser as $uir) {
						$htmlRole .= "<option value=\"" . $uir->id . "\">" . $uir->username . "</option>";
					}
				}				
				$htmlRole .= "</select>";
				$htmlRole .= "<td valign=\"top\">";
				$htmlRole .= "<input type=\"button\" style=\"width:100px\" value=\" " . JText :: _("REMOVE") . " &rarr;\" onClick=\"domSwap(document.getElementById('tableOne'), document.getElementById('tableTwo'));\"/><br> <br>";
				$htmlRole .= "<input type=\"button\" style=\"width:100px\" value=\" &larr; " . JText :: _("ADD") . "\" onClick=\"domSwap(document.getElementById('tableTwo'), document.getElementById('tableOne'))\"/><br>";
				$htmlRole .= "</td>";

				$htmlRole .= "<td> \n";
				$htmlRole .= "<select size=\"6\" id=\"tableTwo\" name=\"tableTwo[]\" multiple style=\"width: 200px\">";
				
				if ($events->id == 0){
					$userIsNotRole = $modelEvent->getAllUsers();
				}else{
					$userIsNotRole = $modelEvent->getAllUser($events->id);
				}					
				if (count($userIsNotRole) > 0){
					foreach ($userIsNotRole as $uinr) {
						$htmlRole .= "<option value=\"" . $uinr->id . "\">" . $uinr->username . "</option>";
					}	
				}
				$htmlRole .= "</select>";
				$htmlRole .= "</td> \n";
				$htmlRole .= "</tr> \n";
				$htmlRole .= "</table> \n";
				
				//assign template

				$this->assignRef ( 'events', $events );
				$this->assignRef ( 'fillDataEventtype', $fillDataEventtype );
				$this->assignRef ( 'fillAsset', $fillAsset );
				$this->assignRef('htmlRole', $htmlRole);

				break;

			default :
				global $mainframe, $option;
				//initialise variables
				//$document	= & JFactory::getDocument();
				$db			= & JFactory::getDBO();
				//$user 		= & JFactory::getUser();

				//get vars
				$filter_order		= $mainframe->getUserStateFromRequest( $option.'.events.filter_order', 'filter_order', 	'subject', 'cmd' );
				$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.'.events.filter_order_Dir', 'filter_order_Dir', '', 'word' );
				$search 			= $mainframe->getUserStateFromRequest( $option.'.events.search', 'search', '', 'string' );
				$search 			= $db->getEscaped( trim(JString::strtolower( $search ) ) );
				$template			= $mainframe->getTemplate();


				JToolBarHelper::title ( JText::_('PROJECT NAME').' - '.JText::_ ( 'EVENTS MANAGER' ), 'jbschedule' );
				JToolBarHelper::deleteList ( JText::_ ( 'ARE YOU SURE' ) ); //param: 1: message 2: task to do 3: Label of the button
				JToolBarHelper::editListX ();
				JToolBarHelper::addNewX ();
				$modelEvent = $this->getModel('events');

				// Get data from the model				
				$items = & $this->get ( 'Data' );
				//$total      = & $this->get( 'Total');
				$pageNav 	= & $this->get( 'Pagination' );

				//output row html
				$listItem ='';
				for ($i=0;$i<count($items);$i++){
					$row = $items[$i];
					$checked 	= JHTML::_('grid.id',   $i, $row->idevents );
					$link 		= JRoute::_('index.php?option=com_jbschedule&controller=events&task=edit&cid[]='. $row->idevents);
					$listItem .='<tr class="row'.$i.'">';
					$listItem .='<td>'.$row->idevents.'</td>';
					$listItem .='<td>'.$checked.'</td>';
					$listItem .='<td><a href='.$link.'>'.$row->subject.'</a></td>';
					$listItem .='<td>'.$row->content.'</td>';
					$listItem .='<td>'.$row->title.'</td>';

					$assetGroup = $modelEvent->getAllAssetByEvent($row->idevents);
					$nameAsset = '';
					if (count($assetGroup)){
						foreach ($assetGroup as $n){
							$nameAsset.=' '.$n->name.' ,';
						}
					}
					$listItem .='<td>'.substr($nameAsset,0,strlen($nameAsset)-1).'</td>';
					$listItem .='<td>'.$row->username.'</td>';
					$listItem .='<td>'.$row->email.'</td>';
					$listItem .='<td>'.$row->start_date.'</td>';
					$listItem .='<td>'.$row->end_date.'</td>';
					$listItem .='</tr>';
				}

				// table ordering
				$lists['order_Dir'] = $filter_order_Dir;
				$lists['order'] = $filter_order;

				// search filter
				$lists['search']= $search;

				$this->assignRef ( 'items', $items );
				$this->assignRef ( 'listItem', $listItem);
				$this->assignRef ( 'lists', $lists);
				$this->assignRef ( 'pageNav' , $pageNav);
				$this->assignRef ( 'template', $template);
		}
		parent::display ( $tpl );
	}

	function checkIdAssetInArray($idAsset,$dataEventsAssets){
		$ok = false;
		if (count($dataEventsAssets) >0){
			foreach ($dataEventsAssets as $data){
				if ($data->asset_id == $idAsset){
					$ok = true;
				}
			}
		}
		return $ok;
	}

}
